package sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sys.core.ResponseResult;
import sys.dao.OperationMapper;
import sys.entity.OperationEntity;
import sys.entity.PageData;
import sys.service.OperationService;

import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OperationServiceImpl implements OperationService {

    @Autowired
    private OperationMapper operationMapper;


 /*   @Override
    public List<OperationEntity> select(OperationEntity entity) {
        return operationDao.select(entity);
    }

    @Override
    public PageData<OperationEntity> likeSelect(OperationEntity entity) {
        return PageUtil.getPageData(entity, operationDao);
    }*/

    @Override
    public void exportExcel(OperationEntity entity, HttpServletResponse response) {
        // 获取头部信息（可以设置为动态）
        String[] headList = new String[]{"主键", "操作名称", "操作类", "操作方法"};

        String[] headEngList = new String[]{"id", "operName", "operClass", "operMethod"};

        String[] describeList = new String[]{"", "", "", ""};

        //设置头部以及描述信息
        Map<String, String> headAndDescribeMap = new LinkedHashMap<>();
        for (int i = 0; i < headEngList.length; i++) {
            headAndDescribeMap.put(headEngList[i], describeList[i]);
        }

        // ExcelUtil.exportExcel(entity, response, operationDao, headList, headAndDescribeMap, "积云");

    }

    @Override
    public ResponseResult update(OperationEntity operationEntity) {
        int i = operationMapper.updateById(operationEntity);
        if (i > 0) {
            return ResponseResult.success("修改成功");
        }
        return ResponseResult.failed("修改失败");
    }

    @Override
    public PageData<OperationEntity> selectByOperName(OperationEntity operationEntity) {

        //QueryWrapper<OperationEntity> queryWrapper = new QueryWrapper();
        LambdaQueryWrapper<OperationEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (operationEntity.getOperName() != null && !operationEntity.getOperName().equals("")) {
            //queryWrapper.like("oper_name", operationEntity.getOperName());
            lambdaQueryWrapper.like(OperationEntity::getOperName, operationEntity.getOperName());
        }
        if (operationEntity.getOperClass() != null && !operationEntity.getOperClass().equals("")) {
            lambdaQueryWrapper.eq(OperationEntity::getOperClass, operationEntity.getOperClass());
        }
        if (operationEntity.getOperMethod() != null && !operationEntity.getOperMethod().equals("")) {
            lambdaQueryWrapper.eq(OperationEntity::getOperMethod, operationEntity.getOperMethod());
        }
        if (operationEntity.getCreateUser() != null) {
            lambdaQueryWrapper.gt(OperationEntity::getCreateUser, operationEntity.getCreateUser());
        }

        // lambdaQueryWrapper.orderByAsc(OperationEntity::getCreateUser);
        //分页查询

        if (operationEntity.getPageSize() == null) {

        }
        IPage<OperationEntity> page = new Page<OperationEntity>(operationEntity.getCurrentPage(), operationEntity.getPageSize() == null ? 10 : operationEntity.getPageSize());
        IPage<OperationEntity> pages = operationMapper.selectPage(page, lambdaQueryWrapper);
        List<OperationEntity> operationEntityList = pages.getRecords();
        log.info("pages:{}", pages.toString());
        // List<OperationEntity> operationEntityList = operationMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(operationEntityList)) {
            return null;
        }
        PageData<OperationEntity> pageData = new PageData<>();
        pageData.setResult(operationEntityList);
        pageData.setCurrentPage(operationEntity.getCurrentPage());
        pageData.setCount((int) pages.getTotal());
        pageData.setTotalPage((int) pages.getPages());
        return pageData;
    }


}
